#include "vmStartTools.h"
#include <qfiledialog.h>
#include <Windows.h>
#include <qmessagebox.h>


vmStartTools::vmStartTools(QWidget *parent)
    : QWidget(parent)
{
    ui.setupUi(this);
    setWindowFlags(Qt::WindowCloseButtonHint);
    readAllHost();
}

vmStartTools::~vmStartTools()
{}

void vmStartTools::on_removeHost_clicked()
{
    QSettings settings("./Start/vHost.ini", QSettings::IniFormat);
    auto row = ui.listWidget->currentRow();
    if (row < 0)
        return;
    ui.listWidget->takeItem(row);
    settings.remove("HostList/host"+QString::number(row));
    settings.remove("HostList/host" + QString::number(row+1)); // 删除重复值&移动元素
    on_save_clicked(); // 保存现有元素
}

void vmStartTools::on_closeAStart_clicked()
{ 
    QString appName = "vHost";
    QSettings* reg = new QSettings("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat);
    reg->remove(appName);
    reg->deleteLater(); 
    QMessageBox::information(this, "vmStartTools", "关闭成功");
}

void vmStartTools::on_startAStart_clicked()
{
    QString appName = "vHost";
    QString appPath = QApplication::applicationDirPath() + "/vHost.exe.lnk"; // 不是链接找不到配置文件
    appPath.replace("/","\\");
    appPath.insert(0,"\"");
    appPath.append("\"");
    QSettings* reg = new QSettings("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat);
    QString val = reg->value(appName).toString(); // 获取当前注册表中的值  如果不一致，则设置新的路径
    if (val != appPath) {
        reg->setValue(appName, appPath);
    }
    reg->deleteLater(); 
    QMessageBox::information(this, "vmStartTools", "开启成功");
}

void vmStartTools::on_save_clicked()
{
    // 没有需要保存的直接返回
    if (ui.listWidget->count() < 0)
    {
        QMessageBox::information(this, "vmStartTools", "保存成功");
        return;
    }
    QSettings settings("./Start/vHost.ini", QSettings::IniFormat);
    for (int i = 0; i < ui.listWidget->count(); i++)
    {
        auto items = ui.listWidget->item(i);
        auto value = items->text();
        settings.setValue("HostList/host" + QString::number(i), value);
    }
    QMessageBox::information(this, "vmStartTools", "保存成功");
}

void vmStartTools::on_getVMsoftware_clicked()
{
    QFileDialog fDialog;
    auto vmName = fDialog.getExistingDirectory(this, "选择虚拟机软件所在文件夹", "./");
    if (vmName == "")
        return;
    QString cl = "setx PATH \"%PATH%;";
    cl.append(vmName);
    cl.append("\"");
    auto code = system(cl.toLatin1());
    if (code == 0)
        QMessageBox::information(this,"vmStartTools","成功获取");
    else
        QMessageBox::information(this, "vmStartTools", "获取失败");
  
    // 创建链接文件
    QString  linkCreate = "createlink.vbs ";
    linkCreate.append("\""); 
    linkCreate.append(QApplication::applicationDirPath() + "/vHost.exe");
    linkCreate.append("\"");
    linkCreate.replace("/", "\\");
    system(linkCreate.toLatin1());
}

void vmStartTools::readAllHost()
{
    QSettings settings("./Start/vHost.ini", QSettings::IniFormat);
    constexpr int MAXHOST = 100;
    for (int i = 0; i < MAXHOST; i++)
    {
       auto value =  settings.value("HostList/host"+QString::number(i), "").toString();
       if (value == "")
           return;
       auto items = new QListWidgetItem(ui.listWidget);
       items->setText(value);
       ui.listWidget->addItem(items);
    }

}

void vmStartTools::on_addHost_clicked()
{
    QFileDialog fDialog;   
    auto vmxName =  fDialog.getOpenFileName(this, "选择虚拟机", "./", "vmFile (*.vmx)");
    if (vmxName == "")
        return;
    auto items = new QListWidgetItem(ui.listWidget);
    items->setText(vmxName);
    ui.listWidget->addItem(items);
}
